package com.vlong.town.service.goodsType;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vlong.town.entity.goodsType.GoodsType;
import com.vlong.town.mapper.goodsType.GoodsTypeMapper;

/**
 * @author Assassin
 */
@Service
public class GoodsTypeService{

	// Fields

	@Autowired
	private GoodsTypeMapper goodsTypeMapper;// DAO接口

	// Methods

   /**
	* 添加
	* @param typeId 
	* @param typeName 
	* @param typePid 
	* @param typeStatus 
	* @param typeOrder 
	* @param typeLevel 
	* @param isDel 
	* @param isParent 
	* @param createTime 
	* @param updateTime 
	* @return Long typeId 添加后的数据库主键值
	*/
	@Transactional
	public Long add(Long typeId, String typeName, Long typePid, Integer typeStatus, Integer typeOrder, Integer typeLevel, Integer isDel, Integer isParent, String typeDesc,Long createTime, Long updateTime) {
		// 构造参数实体
		GoodsType goodsType = new GoodsType();
		goodsType.setTypeId(typeId);// Set 
		goodsType.setTypeName(typeName);// Set 
		goodsType.setTypePid(typePid);// Set 
		goodsType.setTypeStatus(typeStatus);// Set 
		goodsType.setTypeOrder(typeOrder);// Set 
		goodsType.setTypeLevel(typeLevel);// Set 
		goodsType.setIsDel(isDel);// Set 
		goodsType.setIsParent(isParent);// Set
		goodsType.setTypeDesc(typeDesc);
		goodsType.setCreateTime(createTime);// Set 
		goodsType.setUpdateTime(updateTime);// Set 
		// 调用DAO接口
		this.goodsTypeMapper.insert(goodsType);
		
		return goodsType.getTypeId();
	}

   /**
	* 删除
	* @param typeId 主键值
	*/
	@Transactional
	public void delete(Long typeId) {
		// 调用DAO接口
		this.goodsTypeMapper.delete(typeId);
	}

   /**
	* 更新
	* @param typeId 
	* @param typeName 
	* @param typePid 
	* @param typeStatus 
	* @param typeOrder 
	* @param typeLevel 
	* @param isDel 
	* @param isParent 
	* @param createTime 
	* @param updateTime 
	*/
	@Transactional
	public void edit(Long typeId, String typeName, Long typePid, Integer typeStatus, Integer typeOrder, Integer typeLevel, Integer isDel, Integer isParent, String typeDesc,Long createTime, Long updateTime) {
		// 构造参数实体
		GoodsType goodsType = new GoodsType();
		goodsType.setTypeId(typeId);// Set 
		goodsType.setTypeName(typeName);// Set 
		goodsType.setTypePid(typePid);// Set 
		goodsType.setTypeStatus(typeStatus);// Set 
		goodsType.setTypeOrder(typeOrder);// Set 
		goodsType.setTypeLevel(typeLevel);// Set 
		goodsType.setIsDel(isDel);// Set 
		goodsType.setIsParent(isParent);// Set 
		goodsType.setTypeDesc(typeDesc);
		goodsType.setCreateTime(createTime);// Set 
		goodsType.setUpdateTime(updateTime);// Set 
		// 调用DAO接口
		this.goodsTypeMapper.update(goodsType);
	}

   /**
	* 主键查询
	* @param typeId 主键值
	* @return GoodsType 实体
	*/
	@Transactional(readOnly = true)
	public GoodsType queryById(Long typeId) {
		// 调用DAO接口
		return this.goodsTypeMapper.selectById(typeId);
	}

   /**
	* 根据条件查询列表
	* @param typeId 
	* @param typeName 
	* @param typePid 
	* @param typeStatus 
	* @param typeOrder 
	* @param typeLevel 
	* @param isDel 
	* @param isParent 
	* @param createTime 
	* @param updateTime 
	* @return List<GoodsType> 实体List
	*/
	@Transactional(readOnly = true)
	public List<GoodsType> queryList(Long typeId, String typeName, Long typePid, Integer typeStatus, Integer typeOrder, Integer typeLevel, Integer isDel, Integer isParent, String  typeDesc,Long createTime, Long updateTime) {
		// 构造参数实体
		GoodsType goodsType = new GoodsType();
		goodsType.setTypeId(typeId);// Set 
		goodsType.setTypeName(typeName);// Set 
		goodsType.setTypePid(typePid);// Set 
		goodsType.setTypeStatus(typeStatus);// Set 
		goodsType.setTypeOrder(typeOrder);// Set 
		goodsType.setTypeLevel(typeLevel);// Set 
		goodsType.setIsDel(isDel);// Set 
		goodsType.setIsParent(isParent);// Set 
		goodsType.setTypeDesc(typeDesc);
		goodsType.setCreateTime(createTime);// Set 
		goodsType.setUpdateTime(updateTime);// Set 
		// 调用DAO接口
		return this.goodsTypeMapper.selectList(goodsType);
	}
	
	@Transactional(readOnly = true)
	public List<GoodsType> querySubList(Long typePid) {
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("typePid", typePid);
		paramsMap.put("isDel", 0);
		return this.goodsTypeMapper.selectByPid(paramsMap);
	}
	
	
	@Transactional
	public void editIsDel(Long typeId, Integer isDel) {
		GoodsType goodsType = goodsTypeMapper.selectById(typeId);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("typeId", typeId);
		paramsMap.put("isDel", isDel);
		paramsMap.put("updateTime", System.currentTimeMillis());
		goodsTypeMapper.updateIsDel(paramsMap);
		
		if(isDel.intValue()==1){
			// 更新父级分类信息
			if(goodsType.getTypePid() != null && goodsType.getTypePid() .longValue() != 0L) {
				List<GoodsType> psubList = querySubList(goodsType.getTypePid());
				if(psubList == null || psubList.size() == 0) {
					this.editIsParent(goodsType.getTypePid(), 0);
				}
			}
			// 更新子级机构信息->同步所有子机构删除状态
			if(goodsType.getIsParent().intValue() == 1) {
				HashMap<String, Object> subParamsMap = new HashMap<String, Object>();
				subParamsMap.put("typePid", goodsType.getTypeId());
				subParamsMap.put("isDel", isDel);
				subParamsMap.put("updateTime", System.currentTimeMillis());
				this.goodsTypeMapper.updateIsDelByPid(subParamsMap);
			}
			// 所有子集都删除后，更新自身是否父级属性
			this.editIsParent(typeId, 0);
		}
		
	}
	
	
	@Transactional
	public void editIsParent(Long typeId, int isParent) {
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("typeId", typeId);
		paramsMap.put("isParent", isParent);
		paramsMap.put("updateTime", System.currentTimeMillis());
		goodsTypeMapper.updateIsParent(paramsMap);
	}

}